package com.oc.claimobs.bb;

import javax.microedition.location.Location;

import net.rim.device.api.ui.Screen;

import com.oc.claimobs.bb.facade.InfoService;
import com.oc.claimobs.bb.model.Info;
import com.oc.claimobs.bb.screens.ConfirmScreen;
import com.oc.claimobs.bb.screens.DireccionScreen;
import com.oc.claimobs.bb.screens.InfoScreen;
import com.oc.claimobs.bb.screens.MenuScreen;
import com.oc.claimobs.bb.screens.SendingScreen;
import com.oc.claimobs.bb.screens.camera.legacy.PreviewPictureLegacyScreen;

public class ScreenFlow {

	private static ScreenFlow instance = new ScreenFlow();

	public static ScreenFlow getInstance() {
		return instance;
	}
	
	public static void restartTheFlow(){
		instance = null;
	}

	// States
	public static final int BEGINING = 0;
	public static final int FINISH = -1;

	public static final int SETUP = 10;
	public static final int MENU = 20;
	public static final int FOTO = 30;
	public static final int DIRECCION = 40;
	public static final int PREVIEW = 50;
	public static final int CONFIRMA = 60;

	// Instances
	private Screen SETUP_PAGE = null;
	private Screen MENU_PAGE = null;
	private Screen FOTO_PAGE = null;
	private Screen DIRECCION_PAGE = null;
	private Screen SENDING_PAGE = null;
	private Screen CONFIRMA_PAGE = null;

	// Lazy Init
	public Screen getSETUP_PAGE() {
		if (SETUP_PAGE == null) {
			SETUP_PAGE = new InfoScreen();
		}
		return SETUP_PAGE;
	}

	public Screen getMENU_PAGE() {
		if (MENU_PAGE == null) {
			MENU_PAGE = new MenuScreen();
		}
		return MENU_PAGE;
	}

	public Screen getFOTO_PAGE() {
		if (FOTO_PAGE == null) {
			//FOTO_PAGE = new PreviewPictureScreen();
			FOTO_PAGE = new PreviewPictureLegacyScreen();
		}
		return FOTO_PAGE;
	}

	public Screen getDIRECCION_PAGE() {
		if (DIRECCION_PAGE == null) {
			DIRECCION_PAGE = new DireccionScreen();
		}
		return DIRECCION_PAGE;
	}

	public Screen getSENDING_PAGE() {
		if (SENDING_PAGE == null) {
			SENDING_PAGE = new SendingScreen();
		}
		return SENDING_PAGE;
	}

	public Screen getCONFIRMA_PAGE() {
		if (CONFIRMA_PAGE == null) {
			CONFIRMA_PAGE = new ConfirmScreen();
		}
		return CONFIRMA_PAGE;
	}

	// Wires
	int currentState = BEGINING;

	int currentSession = 0;

	public Screen getNext(String message) {

		int previousState = currentState;

		try {

			if (currentState == BEGINING) {
				currentState = FOTO;
				return getFOTO_PAGE();
			}

			if (currentState == FOTO) {

				InfoService service = new InfoService();
				Info o = service.getByKey(new Integer(1));
				if (o != null && o.getNombre() != null & o.getNombre().length() > 0) {
					currentState = DIRECCION;
					return getDIRECCION_PAGE();
				} else {
					currentState = SETUP;
					return getSETUP_PAGE();
				}
			}

			if (currentState == SETUP) {
				currentState = DIRECCION;
				return getDIRECCION_PAGE();
			}

			if (currentState == DIRECCION) {
				if (message.equalsIgnoreCase("success")) {
					currentState = PREVIEW;
					return getSENDING_PAGE();
				} else if (message.equalsIgnoreCase("setup")) {
					currentState = SETUP;
					return getSETUP_PAGE();
				}
			}

			if (currentState == PREVIEW) {
				if (message.equalsIgnoreCase("success")) {
					currentState = CONFIRMA;
					return getCONFIRMA_PAGE();
				} else if (message.equalsIgnoreCase("cancel")) {
					currentState = FINISH;
					//return getFOTO_PAGE();
				}
			}

			// Final state
			if (currentState == CONFIRMA) {
				currentState = FINISH;
				//return null;
			}

			// Final state
			if (currentState == FINISH) {
				currentSession++;
				cleanSession();
				currentState = FOTO;
				
				PreviewPictureLegacyScreen pps = (PreviewPictureLegacyScreen)getFOTO_PAGE();
				pps.resetPreview();
				
				return pps;
			}

		} catch (Exception ex) {
			currentState = previousState;
		}

		return null;
	}
	
	public void cleanSession(){
		SETUP_PAGE = null;
		MENU_PAGE = null;
		//FOTO_PAGE = null;
		DIRECCION_PAGE = null;
		SENDING_PAGE = null;
		CONFIRMA_PAGE = null;
	}
	

	public int getCurrentSession() {
		return currentSession;
	}

	//

	private byte[] lastPicture = null;

	public byte[] getLastPicture() {
		return lastPicture;
	}

	public void setLastPicture(byte[] lastPicture) {
		this.lastPicture = lastPicture;
	}

	private Location currentLocation = null;

	public Location getCurrentLocation() {
		return currentLocation;
	}

	public void setCurrentLocation(Location currentLocation) {
		this.currentLocation = currentLocation;
	}

}
